package com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.repository;

import com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.entity.AreaRatio;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:
 * @CreateTime: 2024/7/17 10:27
 * @Description:
 */
@Repository
public class AreaRatioRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public AreaRatio findByCondition(String province, String city, String county, String structureType) {
        String sql = "select * from xczh_area_ratio where  province = ? and city = ? and county = ? and structure_type = ? ";
        AreaRatio areaRatio;
        try {
            areaRatio = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(AreaRatio.class), province, city, county, structureType);
            return areaRatio;
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public void save(AreaRatio areaRatio) {

        String sql = "insert into xczh_area_ratio values(:id,:province,:city,:county,:structureType," +
                ":structureType1,:year,:use,:cs,:createUser,:createTime,:updateUser,:updateTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(areaRatio);
        namedParameterJdbcTemplate.update(sql, source);

    }


    public void update(AreaRatio areaRatio) {
        String sql = "update xczh_area_ratio set year= :year ,use = :use ,cs = :cs " +
                ",update_user = :updateUser ,update_time = :updateTime where id = :id";
        SqlParameterSource source = new BeanPropertySqlParameterSource(areaRatio);
        namedParameterJdbcTemplate.update(sql, source);
    }

    public List<AreaRatio> getAreaRatioByCondition(String province, String city, String county) {
        String sql = "select * from xczh_area_ratio where  province = ? and city = ? and county = ? ";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(AreaRatio.class), province, city, county);
    }
}
